﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TruckScale.Helpers;
using static TruckScale.Helpers.LogHelper;

namespace TruckScale.Models
{
  public class PriceModel
  {
    public int? AutoNo { get; set; }
    public int? CustomerId { get; set; }
    public int? GoodsId { get; set; }
    public string Price { get; set; }

    public static List<PriceModel> SelectPrice()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "select * from price;";
          IEnumerable<PriceModel> output = cnn.Query<PriceModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static List<PriceModel> SelectPrice(int? wz, int? kh)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select * from price where goods_id = {wz} and customer_id = {kh};";
          IEnumerable<PriceModel> output = cnn.Query<PriceModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static int SelectExistPrice(PriceModel price)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select count(*) from price where goods_id = '{price.GoodsId}' and customer_id = '{price.CustomerId}' and auto_no != '{price.AutoNo}';";
          int output = cnn.Query<int>(sql).FirstOrDefault();
          return output;
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int InsertPrice(PriceModel price)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "insert into price (goods_id, customer_id, price) values (@GoodsId, @CustomerId, @Price);";
          return cnn.Execute(sql, price);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int UpdatePrice(PriceModel price)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "update price set goods_id = @GoodsId, customer_id = @CustomerId, price = @Price where auto_no = @AutoNo;";
          return cnn.Execute(sql, price);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int DeletePrice(PriceModel price)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"delete from price where auto_no = '{price.AutoNo}'";
          return cnn.Execute(sql);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
  }
}
